package com.gxuwz.attend.dao;

import com.gxuwz.attend.entity.Admin;
import com.gxuwz.attend.entity.Instructor;
import com.gxuwz.attend.entity.Student;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;
import java.util.Map;

/**
 * 访问用户数据DAO类，封装访问数据表的常用CRUD方法
 */
public class LoginDao {
    private JdbcTemplate jdbcTemplate;


    public List<Admin> getAllUsers(int size, int offset) {
        System.out.println("dao:" + size);
        System.out.println("dao:" + offset);

        String sql = "SELECT * FROM sys_user LIMIT ? OFFSET ?";
        return jdbcTemplate.query(sql, new Object[]{size, offset}, (rs, rowNum) -> {
            Admin admin = new Admin();
            admin.setUserid(rs.getString("userid")); // 假设数据库中有 "userid" 字段
            admin.setFullname(rs.getString("fullname")); // 假设数据库中有 "fullname" 字段
            admin.setPassword(rs.getString("password")); // 假设数据库中有 "password" 字段
            admin.setTelephone(rs.getString("telephone")); // 假设数据库中有 "telephone" 字段
            return admin;
        });
    }


    public List<Admin> findUsersByUsername(String username, int size, int offset) {

        String sql = "SELECT * FROM sys_user WHERE fullname LIKE ? LIMIT ? OFFSET ?";
        return jdbcTemplate.query(sql, new Object[]{"%" + username + "%", size, offset}, (rs, rowNum) -> {
            Admin admin = new Admin();
            admin.setUserid("id");
            admin.setFullname(rs.getString("fullname"));
            admin.setPassword(rs.getString("password"));
            admin.setTelephone(rs.getString("telephone"));
            return admin;
        });
    }


    public int delete(String id) {
        String sql = "DELETE FROM sys_user WHERE userid = ?";
       return jdbcTemplate.update(sql, id);
    }
    public Admin loginAdmin(String username, String password) {
        String sql = "SELECT * FROM sys_user WHERE userid=? AND password=?";

        List<Admin> admin =  jdbcTemplate.query(sql, new Object[]{username, password}, (rs, rowNum) -> new Admin(
                rs.getString("userid"),
                rs.getString("fullname"),
                rs.getString("password"),
                rs.getString("telephone")
        ));
        return admin.isEmpty() ? null : admin.get(0);
    }


    public Instructor loginInstructor(String username, String password) {
        String sql = "SELECT * FROM sys_instructor WHERE instID=? AND password=?";
        List<Instructor> instructors = jdbcTemplate.query(sql, new Object[]{username, password},
                (rs, rowNum) -> new Instructor(
                        rs.getString("instID"),
                        rs.getString("password"),
                        rs.getString("instName"),
                        rs.getString("depID"),
                        rs.getString("telephone")
                ));

        return instructors.isEmpty() ? null : instructors.get(0); // 如果查询结果为空，返回 null
    }
    public Student loginStudent(String username, String password) {
        String sql = "SELECT * FROM sys_student WHERE stuID=? AND password=?";
        List<Student> students = jdbcTemplate.query(sql, new Object[]{username, password},
                (rs, rowNum) -> new Student(
                        rs.getString("stuID"),
                        rs.getString("password"),
                        rs.getString("classID"),   // 班级 ID
                        rs.getString("stuName"),    // 学生姓名
                        rs.getString("sex"),        // 性别
                        rs.getString("address"),    // 地址
                        rs.getString("stuTel"),     // 学生电话
                        rs.getString("contact"),    // 联系人
                        rs.getString("contactTel")  // 联系人电话
                ));

        return students.isEmpty() ? null : students.get(0); // 如果查询结果为空，返回 null
    }



    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
}
